<?php

namespace app\admin\controller;

use app\_interface\ResourceInterface;
use app\common\entity\ResponseData;
use Sffi\ElementPlus\ElForm;
use Sffi\ElementPlus\ElForm\ElInput;
use Sffi\ElementPlus\ElForm\ElSelect;
use Sffi\ElementPlus\ElForm\ElSwitch;
use Sffi\ElementPlus\ElForm\ElTextArea;
use Sffi\ElementPlus\ElList;
use think\facade\Request;

class Routes extends \app\admin\controller\AdminBase implements ResourceInterface
{

    public function list()
    {
        $routesModel = \app\model\Routes::when(input('name'), fn($query) => $query->scope('name', input('name')))
            ->page(input('page', 1), input('size', 10))->field('id,name,path,componentStr,create_time,status');
        $count = $routesModel->count();
        $routes = $routesModel->select();
        return ResponseData::success($routes, ['sum' => $count, 'page' => input('page', 1), 'size' => input('size', 10)]);
    }

    public function detail()
    {
        $id = input('id');
        $routes = \app\model\Routes::find($id);
        $routes->meta = json_encode($routes->meta);
        return ResponseData::success($routes);
    }

    public function add()
    {
        $data = Request::only(['name', 'path', 'componentStr', 'meta', 'redirect', 'pid', 'status']);
        return ResponseData::success(\app\model\Routes::create($data));
    }

    public function update()
    {
        $data = Request::only(['name', 'path', 'componentStr', 'meta', 'redirect', 'pid', 'status']);
        $id = input('id');
        $routes = \app\model\Routes::find($id);
        return ResponseData::success($routes->save($data));
    }

    public function delete($id)
    {
        $routes = \app\model\Routes::find($id);
        return ResponseData::success($routes->delete());
    }

    public function getListColumns()
    {
        $list = new ElList();
        $statusColumn = new ElList\ElSwitch('状态', 'status', 'status', 1, 0);
        $statusColumn->setWidth(150);
        $list->addColumn(new ElList\ElInput('名称', 'name', 'name'))
            ->addColumn(new ElList\ElInput('路径', 'path', 'path'))
            ->addColumn(new ElList\ElInput('路径', 'componentStr', 'componentStr'))
            ->addColumn(new ElList\Column('创建时间', 'create_time', 'create_time', 150))
            ->addColumn($statusColumn);

        return ResponseData::success($list->__toArray());
    }

    public function getFormColumns()
    {
        $routes = \app\model\Routes::where(['pid' => 0])->select();
        $pidColumn = new ElSelect('上级菜单', 'pid', '请选择上级菜单');

        foreach ($routes as $route) {
            $pidColumn->addOptionByArgs($route['name'], $route['id'], !$route['status']);
        }
        $form = new ElForm();
        $form->addColumn(new ElInput('名称', 'name', '请填写菜单名称'));
        $form->addColumn($pidColumn);
        $form->addColumn(new ElInput('访问路径', 'path', '请填写菜单访问路径'));
        $form->addColumn(new ElInput('组件路径', 'componentStr', '请填写菜单组件路径'));
        $form->addColumn(new ElInput('跳转路径', 'redirect', '请填写菜单跳转路径'));
        $form->addColumn(new ElTextArea('其他设置', 'meta', '请填写其他设置'));
        $form->addColumn(new ElSwitch('状态', 'status', 1, 0, '有效', '无效'));
        $rule = [
            'name' => [
                ['require' => true, 'message' => '请填写菜单名称', 'trigger' => 'blur'],
            ],
        ];
        return ResponseData::success($form->__toArray(), ['rule' => $rule]);
    }
}